package com.cqndt.disaster.device.controller;

import java.util.Map;
import java.util.List;
import java.util.HashMap;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.cqndt.disaster.device.domain.*;
import com.cqndt.disaster.device.util.Result;
import com.cqndt.disaster.device.vo.TabUserVo;
import com.cqndt.disaster.device.vo.TabProjectVo;
import org.springframework.web.bind.annotation.PostMapping;
import com.cqndt.disaster.device.service.TabProjectService;
import com.cqndt.disaster.device.service.TabAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lgb
 * @date 2020-8-6
 * @说明 项目管理
 **/
@RestController
@RequestMapping("/project/")
public class TabProjectController extends BaseController{
	
    @Autowired
    private TabProjectService tabProjectService;
    @Autowired
    private TabAttachmentService tabAttachmentService;
    @PostMapping(value="pointTabProject")
    public Result pointTabProject(TabProjectVo vo){
        TabUserVo userVo = getCurrentUser();
        vo.setUserId(userVo.getId());
        if(null == vo.getLevel()|| vo.getLevel()==""){
            vo.setLevel(userVo.getLevel());
            vo.setAreaCode(userVo.getAreaCode());
        }
        List<TabProjectVo> list = tabProjectService.selectByCondition(vo);
        Result result = new Result();
        result.setData(list);
        return result;
    }
    /**
     * @param vo
     * @return 项目基础信息(参见单位、项目班子、图片、视频)
     **/
    @PostMapping(value="getTabProject")
    public Result getTabDevice(TabProjectVo vo){
        Map<String,Object> map = new HashMap<>();
        Result result = new Result();
        List<TabProjectVo> list = tabProjectService.selectByCondition(vo);
        if(list.size()>0){
            TabProjectVo tabProject = list.get(0);
            map.put("tabProject",tabProject);
            List<TabAttachment> projectImg = tabAttachmentService.selectAttachments(tabProject.getProjectImg());
            List<TabAttachment> projectVideo = tabAttachmentService.selectAttachments(tabProject.getProjectVideo());
            map.put("projectImg",projectImg);
            map.put("projectVideo",projectVideo);
        }
        List<TabProjectCjdw> cjdw = tabProjectService.selectCjdwByProjectId(vo.getId());
        List<TabProjectXmbz> xmbz = tabProjectService.selectXmbzByProjectId(vo.getId());
        map.put("cjdw",cjdw);
        map.put("xmbz",xmbz);
        result.setData(map);
        return result;
    }
    /**
     * @param vo
     * @return 项目列表
     **/
    @PostMapping(value="listTabProject")
    public Result listTabProject(TabProjectVo vo){
        TabUserVo userVo = getCurrentUser();
        vo.setUserId(userVo.getId());
        if(null == vo.getLevel()|| vo.getLevel()==""){
            vo.setLevel(userVo.getLevel());
            vo.setAreaCode(userVo.getAreaCode());
        }
        PageHelper.startPage(vo.getPage(),vo.getLimit());
        PageInfo<TabProjectVo> pageInfo = new PageInfo<TabProjectVo>(tabProjectService.selectByCondition(vo));
        Result result = new Result();
        result.setCount((int)pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }
    /**
     * @param projectId
     * @return 根据项目id获取设备
     **/
    @PostMapping(value="getDeviceByProjectId")
    public Result getDeviceByProjectId(Integer projectId){
        Result result = new Result();
        List<Map<String,Object>> list  = tabProjectService.getDeviceByProjectId(projectId);
        result.setData(list);
        return result;
    }
    /**
     * @param projectId
     * @return 根据项目id获取设备数量、在线数量
     **/
    @PostMapping(value="getDeviceNumByProjectId")
    public Result getDeviceNumByProjectId(Integer projectId){
        Result result = new Result();
        Map<String,Object> map  = tabProjectService.getDeviceNumByProjectId(projectId);
        result.setData(map);
        return result;
    }
    /**
     * @param projectId 项目id
     * @return 根据项目id查询项目文档
     **/
    @PostMapping(value="getTabProjectDatum")
    public Result getTabProjectDatum(Integer projectId){
        Result result = new Result();
        List<Map<String,Object>> list = tabProjectService.selectProjectDatum(projectId);
        result.setData(list);
        return result;
    }
}